package com.party.service.study.impl;

import com.party.mapper.Curriculum2ContentMapper;
import com.party.mapper.User2ContentMapper;
import com.party.model.*;
import com.party.service.study.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 011926 on 2014/9/23.
 */
@Repository
public class User2ContentServiceImpl implements User2ContentService
{
    @Resource
    User2ContentMapper user2ContentMapper;
    @Resource
    LearningContentService learningContentService;
    @Resource
    CurriculumService curriculumService;
    @Resource
    User2curriculumService user2curriculumService;
    @Override
    public int add(User2Content obj) {
        if(obj != null && obj.getLearningcontentid() != null && obj.getLearningcontentid() > 0 && obj.getUid() != null && obj.getUid() > 0)
        {
            return this.user2ContentMapper.insert(obj);
        }
        return 0;
    }

    @Override
    public int delete(Integer uId,Integer contentId) {
        if(uId != null && uId > 0 && contentId != null && contentId > 0)
        {
            User2ContentExample example = new User2ContentExample();
            User2ContentExample.Criteria criteria = example.createCriteria();
            criteria.andUidEqualTo(uId);
            criteria.andLearningcontentidEqualTo(contentId);
            return this.user2ContentMapper.deleteByExample(example);
        }
        return 0;
    }

    @Override
    public int deleteAllByUid(Integer id) {
        if(id != null && id > 0)
        {
            User2ContentExample example = new User2ContentExample();
            User2ContentExample.Criteria criteria = example.createCriteria();
            criteria.andUidEqualTo(id);
            return this.user2ContentMapper.deleteByExample(example);
        }
        return 0;
    }

    @Override
    public int deleteAllByContentId(Integer id) {
        if(id != null && id > 0)
        {
            User2ContentExample example = new User2ContentExample();
            User2ContentExample.Criteria criteria = example.createCriteria();
            criteria.andLearningcontentidEqualTo(id);
            return this.user2ContentMapper.deleteByExample(example);
        }
        return 0;
    }


    @Override
    public List<User2Content> get(User2Content obj) {
        if(obj != null )
        {
            User2ContentExample example = new User2ContentExample();
            User2ContentExample.Criteria criteria = example.createCriteria();
            if(obj.getId() != null && obj.getId() > 0)
            {
                criteria.andIdEqualTo(obj.getId());
            }
            if(obj.getUid() != null && obj.getUid() > 0)
            {
                criteria.andUidEqualTo(obj.getUid());
            }
            if(obj.getLearningcontentid() != null && obj.getLearningcontentid() > 0)
            {
                criteria.andLearningcontentidEqualTo(obj.getLearningcontentid());
            }
            if(obj.getIsfinished() != null && !"".equals(obj.getIsfinished()))
            {
                criteria.andIsfinishedEqualTo(obj.getIsfinished());
            }
            return this.user2ContentMapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public List<User2Content> get(Integer uid, List<Integer> contentIdList) {
        if(uid != null && uid > 0 && contentIdList != null && contentIdList.size() >0 )
        {
            User2ContentExample example = new User2ContentExample();
            User2ContentExample.Criteria criteria = example.createCriteria();
            criteria.andUidEqualTo(uid);
            criteria.andLearningcontentidIn(contentIdList);
            return this.user2ContentMapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public int update(User2Content obj) {
        if(obj.getId()!=null && obj.getId() > 0 || (obj.getUid() != null&&obj.getUid()>0&&obj.getLearningcontentid()!=null&&obj.getLearningcontentid()>0))
        {
            Integer real = obj.getRealtime();
            Integer time = obj.getTime();
            if(real == null) real = 0;
            if(time == null) real = 0;
            if(real >= time)//学习完成
            {
                obj.setIsfinished("1");
            }
            //通id更新
            if(obj.getUid() > 0)
            {
                return this.user2ContentMapper.updateByPrimaryKeySelective(obj);
            }
            //通过uid和contentId更新
            else{
                User2ContentExample example = new User2ContentExample();
                User2ContentExample.Criteria criteria = example.createCriteria();
                criteria.andUidEqualTo(obj.getUid());
                criteria.andLearningcontentidEqualTo(obj.getLearningcontentid());
                return this.user2ContentMapper.updateByExample(obj, example);
            }
        }
        return 0;
    }

    @Override
    public User2Content get(Integer uid, Integer contentId) {
        if(uid != null && uid >0 && contentId != null && contentId >0)
        {
            User2Content user2Content = new User2Content();
            user2Content.setUid(uid);
            user2Content.setLearningcontentid(contentId);
            List<User2Content> user2Contents = this.get(user2Content);
            if(user2Contents != null && user2Contents.size() > 0)
            {
                return user2Contents.get(0);
            }
        }
        return null;
    }

    @Override
    public int updateTime(Integer uid, Integer contentId, Integer realtime) {
        if(uid != null && contentId != null && realtime != null)
        {
            User2Content user2Content = get(uid,contentId);
            int num = 0;
            if(user2Content != null)//有记录，直接更新
            {
                Integer time = user2Content.getTime();
                if(realtime >= time)
                {
                    user2Content.setIsfinished("1");//设置为已完成
                }
                user2Content.setRealtime(realtime);
                num = this.user2ContentMapper.updateByPrimaryKeySelective(user2Content);//更新记录
            }
            else//没有记录，新增一条
            {
                //查出内容
                LearningContent content = this.learningContentService.getById(contentId);
                if(content != null)
                {
                    user2Content = new User2Content();
                    user2Content.setUid(uid);
                    user2Content.setLearningcontentid(contentId);
                    user2Content.setRealtime(realtime);
                    user2Content.setTime(content.getTime());
                    Integer time = user2Content.getTime();
                    if(realtime >= time)
                    {
                        user2Content.setIsfinished("1");//设置为已完成
                    }
                    num = this.user2ContentMapper.insertSelective(user2Content);
                }
            }

            //判断 课程是否已经全部完成
            if(user2Content.getIsfinished() != null && user2Content.getIsfinished().equals("1"))
            {
                //拿到所有用户学过的内容，并且已经学习完成了的
                User2Content user2Content1 = new User2Content();
                user2Content1.setUid(uid);
                user2Content1.setIsfinished("1");
                List<User2Content> user2ContentList = this.get(user2Content1);
                if(user2ContentList != null && user2ContentList.size()>0)
                {
                    List<Curriculum> curriculumList = this.curriculumService.getByUid(uid);
                    if(curriculumList!=null && curriculumList.size()>0)
                    {
                        for(Curriculum curriculum:curriculumList)
                        {
                            //拿到所有的课程
                            List<LearningContent> contentList = this.learningContentService.getByCurrId(curriculum.getId());
                            if(contentList != null && contentList.size() >0)
                            {
                                boolean pass = true;
                                for(LearningContent content:contentList)
                                {
                                    boolean match = false;//这个内容是否已经学习完了
                                    for(User2Content uc:user2ContentList)
                                    {
                                        if(content.getId() == uc.getLearningcontentid())
                                        {
                                            match = true;
                                            break;
                                        }
                                    }
                                    if(!match)
                                    {
                                        pass = false;
                                        break;
                                    }
                                }
                                if(pass)//这门课程下的所有内容都学习完了
                                {
                                    User2Curriculum user2Curriculum = this.user2curriculumService.get(uid,curriculum.getId());
                                    user2Curriculum.setIsfinished("1");
                                    this.user2curriculumService.update(user2Curriculum);
                                }
                            }

                        }
                    }
                }
            }
            return num;
        }


        return 0;
    }


}
